class SharedController {
    def authenticateService
    def index = {  redirect(action: "list", params: params)}
    // 显示别人共享给我的文件
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100) 
            //println  authenticateService?.userDomain()
            def results = Article.withCriteria {
                shared{
                    eq("id" , authenticateService?.userDomain()?.id)  
                }
				maxResults(20) 
			}
           

			def counts  = Article.withCriteria {                
                shared{
                    eq("id" , authenticateService?.userDomain()?.id)  
                }
				count('id')
			}
        [articleInstanceList: results,   articleInstanceTotal: counts]
    }
// 编辑共享文件，只是简单编辑，不能改变共享人，需要锁定 
    def edit = {
        def articleInstance = Article.get(params.id)
        if (!articleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
        else if(articleInstance.owner ){ //&& articleInstance.owner.id != session.teacherID
            flash.message = "此文章已经被[${articleInstance.owner}]老师在${formatDate(date:articleInstance.lastTime,formatName:'default.datetime.format')}开始编辑。您暂时不能编辑这个文章。"
            redirect(action: "list")
        }else{ 
            
            articleInstance.lastTime = new Date()
            articleInstance.owner = Teacher.get(session.teacherID)
            articleInstance.save()
            return [articleInstance: articleInstance]
        }
    }

    def update = {
        def articleInstance = Article.get(params.id)
        if (articleInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (articleInstance.version > version) {
                    
                    articleInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'article.label', default: 'Article')] as Object[], "Another user has updated this Article while you were editing")
                    render(view: "edit", model: [articleInstance: articleInstance])
                    return
                }
            }
      
            articleInstance.properties = params
            articleInstance.lastTime = new Date()
            articleInstance.owner = null
            if (!articleInstance.hasErrors() && articleInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'article.label', default: 'Article'), articleInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [articleInstance: articleInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
    }

    def refresh = {
        log.info "shared.refresh article ${params.id}."
        if (params.id) {
            def articleInstance = Article.get(params.id)
            articleInstance.lastTime = new Date()
            //articleInstance.owner = Teacher.get(session.teacherID)
            articleInstance.save()
            log.info "shared.refresh article update complete."
        }
    }
     
}
